//#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <ws2tcpip.h>
#define WIN32_LEAN_AND_MEAN
#include"clientSock.h"
#include<string>
#include"src/include/Socket.h"
#include<QDebug>
#include<Buffer.h>
#include"src/include/ThreadPool.h"
#include"recivedata.h"
#include<QThread>


Client::Client() {
    InetAddress address("10.3.13.243",8889);
    if(clntsocket.Connect(address)){
        qDebug()<<"连接成功";
        clnt_fd = clntsocket.getFd();
    }
}

Client::~Client()
{
}

Client &Client::getInstance()
{
    static Client instance;
    return instance;
}

void Client::setThreapool(ThreadPool &pool)
{
    thPool = &pool;
}

void Client::stopThreadPool()
{
    if(thPool){
        thPool->stopPool();
        delete thPool;
        thPool = nullptr;
    }
}

void Client::sendData(const CloudDrive::Packet &packet)
{
//    qDebug()<<"fs"<<QThread::currentThreadId();
    std::vector<uint8_t> buffer = CloudDrive::serialize(packet);
    // 发送
    send(clnt_fd, reinterpret_cast<const char*>(buffer.data()), static_cast<int>(buffer.size()), 0);
}


int Client::getSockfd()
{

    return clnt_fd;
}

void Client::addFunc(std::function<void ()> func)
{
    thPool->addFunc(func);
}

//void Client::connectSever()
//{
//    auto task = [this]{
//        ReceiveData::getInstance().reviveData();
//    };
//    thPool->addFunc(task);
//}
